#include "stdafx.h"
#include "../include/UMyChart9.h"
#include "PaintDialog.h"
#include "KInfoDialog.h"

extern PaintDialog __paint_dialog__;

#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
extern HWND __hwnd_textdialog__;
UMYCHART9_API LRESULT check_mouse_move(UMyChart9*pChart,UMyChart9*pChartDraw,POINT&lastpoint,long*chkIndex,BOOL blBar3,BOOL*blRedraw)
{
	BOOL bInPaintDlg=FALSE;
	POINT pt=lastpoint;
	::ClientToScreen(pChart->m_hWnd,&pt);
	RECT rtDialog={0};
	if (__paint_dialog__.m_hWnd && __paint_dialog__.IsWindowVisible())
	{
		__paint_dialog__.GetWindowRect(&rtDialog);
		if(PtInRect(&rtDialog,pt))
			::ReleaseCapture();
	}

	if (__k_info_dialog__.m_hWnd && __k_info_dialog__.IsWindowVisible())
	{
		__k_info_dialog__.GetWindowRect(&rtDialog);
		if(PtInRect(&rtDialog,pt))
			::ReleaseCapture();
	}

	if(__dialog_activating__&&IsWindowVisible(__dialog_activating__))
	{
		::GetWindowRect(__dialog_activating__,&rtDialog);
		if(PtInRect(&rtDialog,pt))
			::ReleaseCapture();
	}

	if(!memcmp(&lastpoint,&pChart->_pointMouseMove,sizeof(POINT))) return(1);
	RECT rtItem={pChart->_xLeft,pChart->_yTop,
		pChart->_wndWidth-pChart->_xRight,pChart->_wndHeight-pChart->_yBottom};
	BOOL blNew(PtInRect(&rtItem,pChart->_pointMouseMove));
	BOOL blOld(PtInRect(&rtItem,lastpoint));
	memcpy(&lastpoint,&pChart->_pointMouseMove,sizeof(POINT));
	if(chkIndex)
	{
		if(blNew)
		{
			long lIndex(0);
			if(blBar3)
				lIndex=long((lastpoint.x-pChart->_xLeft-pChart->_widthBetween)/(pChart->_widthData+pChart->_widthBetween));
			else
				lIndex=(lastpoint.x-pChart->_xLeft)*pChart->_numTotal/(rtItem.right-rtItem.left);

			if(lIndex>-1&&lIndex<pChart->_numData)
				*chkIndex = lIndex;
			else
				*chkIndex = (blBar3?0:pChart->_numData)-1;
		}
		else
		{
			*chkIndex = (blBar3?0:pChart->_numData)-1;
		}
	}

	if((!blNew)&&blOld) 
	{
		::ReleaseCapture();
	}
	else if(blNew&&!blOld) 
	{
		::SetCapture(pChart->m_hWnd);
	}
	else if(!blNew&&!blOld) 
	{
		return(1);
	}

	if(blRedraw)
		*blRedraw = TRUE;
	else
	{
		HDC wndDC=::GetDC(pChartDraw->m_hWnd);
		if(wndDC)
		{
			pChartDraw->draw_chart(wndDC);
			::ReleaseDC(pChartDraw->m_hWnd,wndDC);
		}
	}
	return(1);
}

